package com.maliu.manager
{
	import flash.geom.Point;
	import mvc.model.ContentModel;
	import flash.events.MouseEvent;
	import com.maliu.ui.ToolTip;
	import com.maliu.utils.HashMap;
	import flash.display.DisplayObject;
	/**
	 * @author maliu
	 */
	public class ToolTipManager
	{
		private static var m_instance:ToolTipManager;
		
		private var m_tipsHashMap:HashMap;
		private var m_tooltip:ToolTip;
		private var m_offsetHashMap:HashMap;
		private var ToolTipRef:Class;
		
		public function ToolTipManager(_param:Param)
		{
			m_tipsHashMap = new HashMap();
			m_offsetHashMap = new HashMap();
			m_tooltip = new ToolTip();
			LayerManager.GetInstance().addToTipsLayer(m_tooltip);
			m_tooltip.visible = false;
		}
		public static function GetInstance():ToolTipManager
		{
			if(!m_instance)
			{
				m_instance = new ToolTipManager(new Param());
			}
			return m_instance;
		}
		//****************** API **************************
		public function addCommonTips(_displayObj:DisplayObject, _contentModel:ContentModel, _offsetX:Number = 0, _offsetY:Number = 0):void
		{
			m_tipsHashMap.put(_displayObj, _contentModel);
			m_offsetHashMap.put(_displayObj, new Point(_offsetX, _offsetY));
			_displayObj.addEventListener(MouseEvent.ROLL_OVER, mouseOverHandler);
			_displayObj.addEventListener(MouseEvent.ROLL_OUT, mouseOutHandler);
		}
		public function addTips(_displayObj:DisplayObject, _contentList:Array, _offsetX:Number = 20, _offsetY:Number = 20):void
		{
			m_tipsHashMap.put(_displayObj, _contentList);
			m_offsetHashMap.put(_displayObj, new Point(_offsetX, _offsetY));
			_displayObj.addEventListener(MouseEvent.ROLL_OVER, tipsMouseOverHandler);
			_displayObj.addEventListener(MouseEvent.ROLL_OUT, tipsMouseOutHandler);
		}
		public function addCustomTips(_displayObj:DisplayObject, _toolTipClass:Class, _contentList:Array, _offsetX:Number = 0, _offsetY:Number = 0):void
		{
		}
		public function removeTips(_displayObj:DisplayObject):void
		{
			trace("[ToolTipManager] --> removeTips() ...");
			if(m_tipsHashMap.containsKey(_displayObj))
			{
				_displayObj.removeEventListener(MouseEvent.ROLL_OVER, mouseOverHandler);
				_displayObj.removeEventListener(MouseEvent.ROLL_OUT, mouseOutHandler);
				
				m_tipsHashMap.remove(_displayObj);
				m_offsetHashMap.remove(_displayObj);
			}
		}
		
		//******************** private method *************************
		private function addListener():void
		{}
		private function removeListener():void
		{}
		private function getToolTipInstance():void
		{
		}
		
		//********************* Handler ****************************
		private function mouseOverHandler(e:MouseEvent):void
		{
			trace("[ToolTipManager] --> mouseOverHandler ...");
			var t_target:DisplayObject = e.currentTarget as DisplayObject;
			var t_content:ContentModel = m_tipsHashMap.getValue(t_target) as ContentModel;
			var t_offset:Point = m_offsetHashMap.getValue(t_target) as Point;
			
			m_tooltip.Title = t_content.title;
			m_tooltip.Rank = t_content.rank;
			m_tooltip.PreCondition = t_content.preCondition;
			m_tooltip.Content = t_content.content;
			
			var t_localPoint:Point = t_target.localToGlobal(new Point(t_target.mouseX, t_target.mouseY));
			m_tooltip.x = t_localPoint.x + t_offset.x;
			m_tooltip.y = t_localPoint.y + t_offset.y;
			m_tooltip.visible = true;
			
			var t_stageWidth:Number = StageManager.GetInstance().stage.stageWidth;
			var t_stageHeight:Number = StageManager.GetInstance().stage.stageHeight;
			if((m_tooltip.x + m_tooltip.width) > t_stageWidth)
			{
				m_tooltip.x = m_tooltip.x - t_offset.x*2 - m_tooltip.width;
			}
			if((m_tooltip.y + m_tooltip.height) > t_stageHeight)
			{
				m_tooltip.y = m_tooltip.y - t_offset.y*2 - m_tooltip.height;
			}
		}
		private function mouseOutHandler(e:MouseEvent):void
		{
			m_tooltip.visible = false;
		}
		private function tipsMouseOverHandler(e:MouseEvent):void
		{
			var t_target:DisplayObject = e.currentTarget as DisplayObject;
			var t_contentList:Array = m_tipsHashMap.getValue(t_target) as Array;
			var t_offset:Point = m_offsetHashMap.getValue(t_target) as Point;
			
			m_tooltip.setContentByList(t_contentList);
			
			
			var t_localPoint:Point = t_target.localToGlobal(new Point(t_target.mouseX, t_target.mouseY));
			m_tooltip.x = t_localPoint.x + t_offset.x;
			m_tooltip.y = t_localPoint.y + t_offset.y;
			m_tooltip.visible = true;
			
			var t_stageWidth:Number = StageManager.GetInstance().stage.stageWidth;
			var t_stageHeight:Number = StageManager.GetInstance().stage.stageHeight;
			if((m_tooltip.x + m_tooltip.width) > t_stageWidth)
			{
				m_tooltip.x = m_tooltip.x - t_offset.x*2 - m_tooltip.width;
			}
			if((m_tooltip.y + m_tooltip.height) > t_stageHeight)
			{
				m_tooltip.y = m_tooltip.y - t_offset.y*2 - m_tooltip.height;
			}
		}
		private function tipsMouseOutHandler(e:MouseEvent):void
		{
			m_tooltip.visible = false;
		}
	}
}
class Param{}
